home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / libs / sphigs / sph_srgp.lha / srgp / examples / flynn.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-01-09  |  4.2 KB  |  151 lines

  1. #include "srgp.h"
  2. #include "geom.h"
  3. #include <stdio.h>
  4.  
  5. /*
  6.  * rasterop.c: PJF 11/16/90
  7.  *
  8.  * fill a window with random colors, allow the user to select a subregion, and
  9.  * track the mouse by rasteropping the subregion along with it.
  10.  *
  11.  */
  12.  
  13. int window_width=100,window_height=100;
  14.  
  15. int windowDidResize(int,int);
  16.  
  17. void acquire_rop_image();
  18. canvasID rop_canvas,backing_canvas;
  19. int w,h;
  20. point old_location;
  21.  
  22. main(argc,argv)
  23. int argc;
  24. char *argv[];
  25. {
  26.    unsigned short r[64],g[64],b[64];
  27.    int i;
  28.  
  29.    /* set up 4x4x4 color cube */
  30.    r[0]=g[0]=b[0]=65535;
  31.    for(i=1;i<64;i++) {
  32.      r[i] = (i/16)*64*256;
  33.      g[i] = ((i&15)/4)*64*256;
  34.      b[i] = (i&3)*64*256;
  35.      };
  36.    
  37.    SRGP_begin("rasterop",window_width,window_height,6,FALSE);
  38.    SRGP_loadColorTable(0,64,r,g,b);
  39.  
  40.    SRGP_allowResize(TRUE);
  41.    SRGP_registerResizeCallback(windowDidResize);
  42.    SRGP_setKeyboardProcessingMode(RAW);
  43.    SRGP_setInputMode(KEYBOARD,EVENT);
  44.  
  45.    windowDidResize(window_width,window_height);
  46.  
  47.  
  48.    /*now, follow the mouse, rop-ing the selected subregion run this in
  49.      event mode, so that we can catch the 'q'-for-quit keypress */
  50.    
  51.    while(TRUE) {
  52.      inputDevice code=SRGP_waitEvent(1);
  53.      locator_measure m;
  54.      if (code == KEYBOARD) {
  55.        char theKey;
  56.        SRGP_getKeyboard(&theKey,2);
  57.        if ((theKey == 'q')||(theKey == 'Q')) {
  58.          SRGP_end();
  59.          exit(0);
  60.          }
  61.        };
  62.      /* do the rop */
  63.      SRGP_sampleLocator(&m);
  64.      if ((old_location.x!=m.position.x)||(old_location.y!=m.position.y)) {
  65.        rectangle oldr=SRGP_defRectangle(old_location.x,old_location.y,
  66.                                         old_location.x+w-1,old_location.y+h-1);
  67.        rectangle newr=SRGP_defRectangle(m.position.x,m.position.y,
  68.                                         m.position.x+w-1,m.position.y+h-1);
  69.        SRGP_useCanvas(0);
  70.        SRGP_copyPixel(backing_canvas,SRGP_defRectangle(0,0,w-1,h-1),
  71.                       old_location);
  72.        SRGP_useCanvas(backing_canvas);
  73.        SRGP_copyPixel(0,newr,SRGP_defPoint(0,0));
  74.        SRGP_useCanvas(0);
  75.        SRGP_copyPixel(rop_canvas,SRGP_defRectangle(0,0,w-1,h-1),m.position);
  76.        old_location=m.position;
  77.        SRGP_refresh();
  78.        };
  79.      };
  80. }
  81.  
  82. int windowDidResize(new_w,new_h)
  83.   int new_w, new_h;
  84.   {
  85.     int i,j;
  86.     rectangle new_clip_rect;
  87.     /* update the canvases' clip rectangle */
  88.     new_clip_rect=SRGP_defRectangle(0,0,new_w-1,new_h-1);
  89.     SRGP_setClipRectangle(new_clip_rect);
  90.     window_width=new_w;
  91.     window_height=new_h;
  92.     /* fill the window with garbage */
  93.     srandom(time(0));
  94.     for(i=0;i<new_w;i+=16)
  95.       for(j=0;j<new_h;j+=16) {
  96.         int color=random()&63;
  97.         rectangle r=SRGP_defRectangle(i,j,i+15,j+15);
  98.         SRGP_setColor(color);
  99.         SRGP_fillRectangle(r);
  100.         };
  101.     SRGP_refresh();
  102.     acquire_rop_image();
  103.   } 
  104.  
  105. void acquire_rop_image()
  106.   {
  107.      point anchor,corner;
  108.      rectangle source_rect;
  109.      char flag=FALSE;
  110.  
  111.      /* rubberband a rectangle */
  112.      SRGP_setColor(0);
  113.      SRGP_setInputMode(LOCATOR,SAMPLE);
  114.      /* loop until the left mouse button goes down */
  115.      while (!flag) {
  116.        locator_measure m;
  117.        SRGP_sampleLocator(&m);
  118.        if (m.button_chord[LEFT_BUTTON]==DOWN) {
  119.          flag=TRUE;
  120.          anchor=m.position;
  121.          };
  122.        };
  123.      SRGP_setLocatorEchoRubberAnchor(anchor);
  124.      SRGP_setLocatorEchoType(RUBBER_RECT);
  125.      while (flag) {
  126.        locator_measure m;
  127.        SRGP_sampleLocator(&m);
  128.        if (m.button_chord[LEFT_BUTTON]==UP) {
  129.          flag=FALSE;
  130.          corner = m.position;
  131.          };
  132.        };
  133.      SRGP_setLocatorEchoType(CURSOR);
  134.      source_rect=GEOM_rectFromDiagPoints(anchor,corner);
  135.      old_location=source_rect.bottom_left;
  136.      SRGP_setLocatorMeasure(old_location);
  137.      w=source_rect.top_right.x-source_rect.bottom_left.x;
  138.      h=source_rect.top_right.y-source_rect.bottom_left.y;
  139.      /* allocate canvases */
  140.      rop_canvas=SRGP_createCanvas(w,h);
  141.      backing_canvas=SRGP_createCanvas(w,h);
  142.      /* rop the portion of the screen into rop_canvas */
  143.      SRGP_useCanvas(rop_canvas);
  144.      SRGP_copyPixel(0,source_rect,SRGP_defPoint(0,0));
  145.      /* and into backing_canvas */
  146.      SRGP_useCanvas(backing_canvas);
  147.      SRGP_copyPixel(0,source_rect,SRGP_defPoint(0,0));
  148.   }
  149.  
  150.  
  151.